package com.loan.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.loan.common.utils.BeanCopyUtils;
import com.loan.common.utils.StringUtils;
import com.loan.system.domain.ConfigEntity;
import com.loan.system.domain.req.ConfigEntityReq;
import com.loan.system.mapper.ConfigMapper;
import com.loan.system.service.AppConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author shiguang
 * @date 2022年09月18日 19:41
 */
@Service
@RequiredArgsConstructor
public class AppConfigServiceImpl implements AppConfigService {

    private final ConfigMapper configMapper;

    @Override
    public ConfigEntityReq getDetail() {
        ConfigEntity configEntity = configMapper.selectOne(new QueryWrapper<ConfigEntity>());
        ConfigEntityReq entityReq = BeanCopyUtils.copy(configEntity, ConfigEntityReq.class);
        if(StringUtils.isNotBlank(configEntity.getFirstApplyCompany())) {
            List<Long> list =  Arrays.asList(configEntity.getFirstApplyCompany().split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
            entityReq.setApplyCompanyList(list);
        }
        return entityReq;
    }

    @Override
    public int insertConfig(ConfigEntityReq config) {
        ConfigEntity configEntity = BeanCopyUtils.copy(config, ConfigEntity.class);
        configEntity.setFirstApplyCompany(StringUtils.join(config.getApplyCompanyList(),','));
        return configMapper.insert(configEntity);
    }

    @Override
    public int updateConfig(ConfigEntityReq config) {
        ConfigEntity configEntity = BeanCopyUtils.copy(config, ConfigEntity.class);
        configEntity.setFirstApplyCompany(StringUtils.join(config.getApplyCompanyList(),','));
        return configMapper.updateById(configEntity);
    }
}
